package com.px.lovepath.ui.home.scrollview;

import com.px.lovepath.R;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
import android.widget.ListView;

public class MenuHorizontalScrollView extends HorizontalScrollView {

	// 菜单状态
	private static boolean mMenuExpansion;
	// 按钮
	private View mMenuButton;
	// 左侧菜单
	private View mMenu;
	private int mScrollToViewPos;
	// 菜单的宽度
	private int mMenuWidth;
	// 当前滑动的位置
	private int mCurrentPos;
	// 手势动作最开始时的x坐标
	private float mLastMotionX = -1;
	// 扩展宽度
	private static final int ENLARGE_WIDTH = 80;
	
	public MenuHorizontalScrollView(Context context) {
		this(context, null);
	}
	public MenuHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return false;
	}
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if(l < (mMenu.getMeasuredWidth() - mMenuButton.getMeasuredWidth() - ENLARGE_WIDTH) / 2){
			mMenuWidth = 0;
		} else{
			mMenuWidth = mMenu.getWidth() - mMenuButton.getMeasuredWidth() - ENLARGE_WIDTH;
		}
		mCurrentPos = l;
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int x = (int) ev.getRawX();
		
		if(ev.getAction() == MotionEvent.ACTION_DOWN) {
			/*手指按下时的x坐标*/
			this.mLastMotionX = (int) ev.getRawX();
		}
		if((mCurrentPos == 0 && x < mScrollToViewPos) || 
				(this.mCurrentPos == mScrollToViewPos * 2 && x > ENLARGE_WIDTH)) {
			return false;
		}
		if(mMenuExpansion == false && this.mLastMotionX > 20){
			return true;
		} else {
			if(ev.getAction() == MotionEvent.ACTION_UP){
				menuSlide();
				return false;
			}
		}
		return super.onTouchEvent(ev);
	}

	// ### 方法 ###
	public void initViews(View[] children, SizeCallBack sizeCallBack, View menu){
		mMenu = menu;
		ViewGroup parent = (ViewGroup) getChildAt(0);
		
		for(int i = 0; i < children.length; i++){
			children[i].setVisibility(View.INVISIBLE);
			parent.addView(children[i]);
		}
		
		OnGlobalLayoutListener onGlLayoutistener = new  MenuOnGlobalLayoutListener(parent, children, sizeCallBack);
		getViewTreeObserver().addOnGlobalLayoutListener(onGlLayoutistener);
		 
	}
	public void clickMenu() {
		if (!mMenuExpansion) {
			mMenuWidth = 0;
		} else {
			mMenuWidth = mMenu.getMeasuredWidth() - mMenuButton.getMeasuredWidth() - ENLARGE_WIDTH;
		}
		menuSlide();
	}
	// ### 方法 ###
	 /**
	  * 设置按钮
	  */
	 public void setMenuView(View btn){
		 this.mMenuButton = btn;
	 } 
	
	// ### 实现  ###
	private void init() {
		this.setHorizontalFadingEdgeEnabled(false);
		this.setVerticalFadingEdgeEnabled(false);
		this.setVisibility(View.INVISIBLE);
		mMenuExpansion = false;
	}
	private MenuHorizontalScrollView getMenuHorizontalScrollView() {
		return this;
	}
	private void menuSlide() {
		if (mMenuWidth == 0) {
			mMenuExpansion = true;
		} else {
			mMenuExpansion = false;
		}
		this.smoothScrollTo(mMenuWidth, 0);
		if (mMenuExpansion == true)
			mMenuButton.setBackgroundResource(R.drawable.home__home_view__setting_collapse);
		else
			mMenuButton.setBackgroundResource(R.drawable.home__home_view__setting_expansion);
	}
	
	// ### 内部类 ###
	public class MenuOnGlobalLayoutListener implements OnGlobalLayoutListener {
		
		private ViewGroup mParent;
		private View[] mChildren;
		private SizeCallBack mSizeCallBack;
		
		public MenuOnGlobalLayoutListener(ViewGroup parent, View[] children, SizeCallBack sizeCallBack) {
			this.mParent = parent;
			this.mChildren = children;
			this.mSizeCallBack = sizeCallBack;
		}

		@Override
		public void onGlobalLayout() {
			final MenuHorizontalScrollView mhscv = getMenuHorizontalScrollView();
			mhscv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
	        mSizeCallBack.onGlobalLayout();
	        mParent.removeViewsInLayout(0, mChildren.length);
	        int width = mhscv.getMeasuredWidth();
	        int height = mhscv.getMeasuredHeight();
	        
	        int[] dims = new int[2];
	        mScrollToViewPos = 0;
	        
	        for(int i = 0; i < mChildren.length; i++){
	        	mSizeCallBack.getViewSize(i, width, height, dims);
	        	mChildren[i].setVisibility(View.VISIBLE);
	            
	        	mParent.addView(mChildren[i], dims[0], dims[1]);
	            if(i == 0){
	                mScrollToViewPos += dims[0];
	            }
	        }
			new Handler().post(new Runnable() {
				@Override
				public void run() {
					mhscv.scrollBy(mScrollToViewPos, 0);

					/* 视图不是中间视图 */
					mhscv.setVisibility(View.VISIBLE);
					mMenu.setVisibility(View.VISIBLE);
				}
			});
	        
	    }
	}
}
